<?php

namespace api\modules\v1\controllers\base;

use common\helpers\CommonHelper;
use common\models\base\CommonStatus;
use services\base\CommonStatusService;
use Yii;
use api\controllers\OnAuthController;
use yii\web\HttpException;

class CommonStatusController extends OnAuthController
{
    public $modelClass = '';

    /**
     * @api {get} /v1/base/common-status/index 基础设置列表
     * @apiGroup CommonStatus
     * @apiName index
     * @apiUse CommonParam
     *
     * @apiParam {String} type      类型【1：（content）内容分类 2：（accounts）账号类型】
     * @apiParam {int} page
     * @apiParam {int} pageSize
     *
     * @apiSuccess {Object}  data
     * @apiSuccess {Object}  data.list             列表数据
     * @apiSuccess {Number}  data.list .id               列表ID
     * @apiSuccess {String}  data.list .type             类型
     * @apiSuccess {String}  data.list .name             名称
     * @apiSuccess {Number}  data.list .status           状态
     * @apiSuccess {String}  data.list .status_name      状态名称
     * @apiSuccess {Number}  data.list .sort             排序
     *
     */
    public function actionIndex()
    {
        $type = Yii::$app->request->get('type');
        if ($type == 'content') {
            $type = CommonStatus::TYPE_CONTENT;
        } elseif ($type == 'accounts') {
            $type = CommonStatus::TYPE_ACCOUNTS;
        } else {
            return CommonHelper::api(422, '类型不存在');
        }

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;
        $commonService = new CommonStatusService();
        return $commonService->commonStatusList($type, $page, $pageSize);
    }

    /**
     * @api {post} /v1/base/common-status/update-common-status 更新基础设置
     * @apiGroup CommonStatus
     * @apiName update-common-status
     * @apiUse CommonParam
     *
     * @apiBody {Number}   id                  ID【编辑时必传】
     * @apiBody {Number}   type                类型【1：（content）内容分类 2：（accounts）账号类型】
     * @apiBody {String}   name                名称
     * @apiBody {Number}   sort                排序
     * @apiBody {Array}    status              状态【0：禁用; 1：启用】
     *
     * @apiSuccess {Number} data    新增/编辑ID
     */
    public function actionUpdateCommonStatus()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['type']) || !$params['type']) {
            return CommonHelper::api(422, '缺少数据类型');
        } else {
            if ($params['type'] == 'content') {
                $params['type'] = CommonStatus::TYPE_CONTENT;
            } elseif ($params['type'] == 'accounts') {
                $params['type'] = CommonStatus::TYPE_ACCOUNTS;
            } else {
                return CommonHelper::api(422, '类型不存在');
            }
        }
        if (!isset($params['name']) || !$params['name']) {
            return CommonHelper::api(422, '名称必填');
        }
        $params['id'] = $params['id'] ?? 0;
        $commonService = new CommonStatusService();
        return $commonService->updateCommonStatus($params);
    }

    /**
     * @api {post} /v1/base/common-status/delete-common-status 删除基础设置（软删除）
     * @apiGroup CommonStatus
     * @apiName delete-common-status
     * @apiUse CommonParam
     *
     * @apiBody {String} id     ID
     *
     * @apiSuccess {Object} data    返回数据
     */
    public function actionDeleteCommonStatus()
    {
        $params = Yii::$app->request->post();
        if (!isset($params['id']) || !$params['id']) {
            return CommonHelper::api(422, '缺少数据ID');
        }

        $commonService = new CommonStatusService();
        return $commonService->deleteCommonStatus($params);
    }
}
